import { menuData,routerData } from "./data";

// 登录admin无数据的封装
function getAllChecks(){
    let arr = []
    menuData.forEach(item=>{
        arr.push(item.id)
        if(item.children && Array.isArray(item.children)){
            item.children.forEach(sitem=>{
                arr.push(sitem.id)
            })
        }
    })

    return arr
}

// 获取菜单
export function getAllMenu() {

    let checkedkeys:string[] = sessionStorage.getItem('checkedkeys')?.split(',')

    if(checkedkeys.length==0){
        checkedkeys = getAllChecks()
    }
    
    let allMenu = [];

    menuData.forEach(item => {
        if (checkedkeys?.includes(item.id)) {
            let objMenu = {...item}
            if (item.children) {
                objMenu.children = []
                item.children.forEach(sitem=>{
                    if(checkedkeys.includes(sitem.id)){
                        objMenu.children.push({...sitem})
                    }
                })
            }
            allMenu.push(objMenu)
        }
    })

    return allMenu
}

// 获取路由
export const getAllRouters=(router)=>{
    let checkedkeys:string[] = sessionStorage.getItem('checkedkeys')?.split(',')

    if(!checkedkeys){
        return
    }
    if(checkedkeys.length == 0 ){
        checkedkeys = getAllChecks()
    }


    routerData.forEach(item=>{
        router.removeRoute(item.name)
    })

    routerData.forEach(item=>{
        if(checkedkeys?.includes(item.id)){
            router.addRoute('layout',{...item})
        }
    })

}